<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form ref="loginformRef" :model="loginform" :rules="loginformRules" label-width="0" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginform.username" prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginform.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
    export default{
        data(){
            return {
                loginform:{
                    username:'admin',
                    password:'123456'
                },
                loginformRules:{
                    username:[
                        {required:true,message:'input username',trriger:blur},
                        {min:3,max:10,message:'3-10 个字符',trriger:blur}
                    ],
                    password:[
                        {required:true,message:'input',trriger:blur}
                    ]
                }
            }
        },
        watch:{
            // "loginform.username":{
            //     handler(newVal,oldVal){
            //         console.log(newVal,oldVal)
            //     }
            // }
            "loginform.username"(newVal,oldVal){
                console.log(newVal,oldVal)
            }
        },
        methods:{
            resetForm(){
                this.$refs.loginformRef.resetFields();
            },
            login(){
                this.$refs.loginformRef.validate(async(valid)=>{
                    if(valid){
                        console.log(true);
                        const {data:result} = await this.$http.post('login',this.loginform);
                        console.log(result);
                        if(result.meta.status != 200){
                            this.$message.info('登录失败')
                        }else{
                            this.$message.success('登录成功')
                            window.sessionStorage.setItem('token',result.data.token)
                            this.$router.push('/home');

                        }
                    }else{
                        console.log(false);
                        return;
                    }
                })
            }
        }
    }
</script>
<style lang="less" scoped>
.login_container {
    position: relative;
    background-color: #2b4b6b;
    height: 100%;
}
.login_box {
    position: absolute;
    top: 50%;
    left:50%;
    transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
}
.avatar_box {
    width: 130px;
    height: 130px;
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 50%;
    // overflow: hidden;
    border: 1px solid #eee;
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
}
.avatar_box img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}
.btns {
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
